package com.dongdongshop.controller;

import cn.hutool.json.JSONUtil;
import com.dongdongshop.data.Result;
import com.dongdongshop.enums.ResultEnum;
import com.dongdongshop.pojo.TbOrder;
import com.dongdongshop.pojo.TbUser;
import com.dongdongshop.service.OrderItemService;
import com.dongdongshop.service.OrderService;
import com.dongdongshop.utils.CookieUtils;
import com.dongdongshop.vo.MyOrderVo;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.shiro.SecurityUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

@RequestMapping("order")
@Controller
public class OrderController {

    @DubboReference
    private OrderService orderService;

    @DubboReference
    private OrderItemService orderItemService;

    @RequestMapping("/orderInfo")
    @ResponseBody
    public Result toOrderInfo(HttpServletRequest request, HttpServletResponse response,
                              @RequestParam(value = "ids") List<Long> ids, String sumPrice){
        //获取用户的登录状态
        TbUser user = (TbUser) SecurityUtils.getSubject().getPrincipal();
        if (user != null){
            String idsStr = JSONUtil.toJsonStr(ids);
//            String sumPriveStr = JSONUtil.toJsonStr(sumPrive);
            String cookieName1 = user.getUsername()+"ids";
            String cookieName2 = user.getUsername()+"sumPrice";
            CookieUtils.setCookie(request,response,cookieName1,idsStr,true);
            CookieUtils.setCookie(request,response,cookieName2,sumPrice,true);
            return Result.OK();
        }
        return Result.response(ResultEnum.ACCOUNT_NOT_LOGIN);
    }
    @RequestMapping("/toOrderInfo")
    public String toOrderInfo(){
        return "getOrderInfo";
    }

    @RequestMapping("/listOrder")
    @ResponseBody
    public Result listOrder(HttpServletRequest request){
        //获取登录的用户
        TbUser user = (TbUser) SecurityUtils.getSubject().getPrincipal();
        String cookieName2 = user.getUsername()+"sumPrice";
        String sumPic = CookieUtils.getCookieValue(request, cookieName2, true);
        return Result.OK().setData(sumPic);
    }

    @RequestMapping("/submitOrder")
    @ResponseBody
    public Result toPay(HttpServletRequest request, HttpServletResponse response,String paymentType,Long addressId){
        //获取登录的用户
        TbUser user = (TbUser) SecurityUtils.getSubject().getPrincipal();
        //转换格式
        String cookieName1 = user.getUsername()+"ids";
        String idsJson = CookieUtils.getCookieValue(request, cookieName1, true);
        List<Long> ids = JSONUtil.toList(idsJson, Long.class);
        //获取总金额
        String cookieName2 = user.getUsername()+"sumPrice";
        String sumPic = CookieUtils.getCookieValue(request, cookieName2, true);

        Map<String,Object> map = orderService.createOrder(user,ids,paymentType,addressId,sumPic);

        return Result.OK().setData(map);
    }
    @RequestMapping("/toOrderSend")
    public String toOrderSend( String out_trade_no, String trade_no, Model model){

        //获取登录的用户
        TbUser user = (TbUser) SecurityUtils.getSubject().getPrincipal();

        model.addAttribute("userId",user.getUsername());
        model.addAttribute("out_trade_no",out_trade_no);
        model.addAttribute("trade_no",trade_no);
        return "home-order-send";
    }
    @RequestMapping("/OrderListFormUser")
    @ResponseBody
    public Result OrderListFormUser(){
        //获取登录的用户
        TbUser user = (TbUser) SecurityUtils.getSubject().getPrincipal();
        //根据用户和订单状态 查出order集合
        List<TbOrder> orderList = orderService.selectOrderByUserId(user.getUsername());
        //根据订单的详情查出我们的订单详情,和商家.
        List<MyOrderVo> myOrderVoList = orderItemService.selectInfoByOrder(orderList);
        return Result.OK().setData(myOrderVoList);
    }
}
